package com.ruibo.modules.system.controller;

import com.ruibo.aop.log.Log;
import com.ruibo.modules.system.criteria.DeptQueryCriteria;
import com.ruibo.modules.system.service.dto.SysDeptDto;
import com.ruibo.modules.system.service.SysDeptService;
import com.ruibo.utils.PageUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * (SysDept)表控制层
 *
 * @author sunfeng
 * @since 2020-04-17 13:35:47
 */
@RestController
@RequestMapping("/system/sysDept")
public class SysDeptController {
    /**
     * 服务对象
     */
    @Resource
    private SysDeptService sysDeptService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @PreAuthorize("@el.check('system:dept:query')")

    public SysDeptDto selectOne(@PathVariable Long id) {
        return this.sysDeptService.findById(id);
    }
    /**
     * 新增部门数据
     */
    @PreAuthorize("@el.check('system:dept:add')")
    @PostMapping
    public ResponseEntity add(@Validated @RequestBody SysDeptDto sysDeptDto) {

        return new ResponseEntity<>(sysDeptService.insert(sysDeptDto), HttpStatus.OK);
    }

    @PreAuthorize("@el.check('system:dept:add')")
    @PutMapping
    public ResponseEntity edit(@Validated @RequestBody SysDeptDto sysDeptDto) {

        return new ResponseEntity<>(sysDeptService.update(sysDeptDto), HttpStatus.OK);
    }

    /**
     * 删除菜单
     */
    @PreAuthorize("@el.check('system:user:remove')")
    @DeleteMapping("/{id}")
    public ResponseEntity<Object> remove(@PathVariable Long id ) {

        return new ResponseEntity<>( sysDeptService.deleteById(id),HttpStatus.OK);
    }

    @Log("查询菜单")
    @GetMapping("/list")
    @PreAuthorize("@el.check('system:dept:list')")
    public ResponseEntity<Object> query(DeptQueryCriteria criteria) throws Exception {
        List<SysDeptDto> deptDtoList = sysDeptService.queryAll(criteria, true);
        return new ResponseEntity<>(PageUtil.toPage(deptDtoList, deptDtoList.size()),HttpStatus.OK);
    }


    /**
     * 获取部门下拉树列表
     */
    @GetMapping("/treeselect")
    public ResponseEntity<Object> treeselect(DeptQueryCriteria dept) {
        List<SysDeptDto> depts = sysDeptService.selectDeptList(dept);
        return new ResponseEntity<Object>(sysDeptService.buildDeptTreeSelect(depts), HttpStatus.OK);
    }
    /**
     * 根据角色id 获取部门列表 以及选中的部门
     */
    @GetMapping(value = "/roleDeptTreeselect/{roleId}")
    public ResponseEntity<Object> roleDeptTreeselect(@PathVariable("roleId") Long roleId) {
        List<SysDeptDto> depts = sysDeptService.findAll();
        HashMap<Object, Object> map = new HashMap<>();
        //  菜单id数组
        Set<Long> collect = sysDeptService.selectDeptListByRoleId(roleId).stream().map(SysDeptDto::getId).collect(Collectors.toSet());
        map.put("checkedKeys", collect);
        map.put("menus", sysDeptService.buildDeptTreeSelect(depts));
        return new ResponseEntity<>(map, HttpStatus.OK);
    }


}
